<?php
include 'includes/application_top.inc';
ini_set('display_errors',1);
//include $_SERVER['DOCUMENT_ROOT']."/urlgateway/common/new_functions.php";
//echo $msisdn."***********";die();
//print_r($_REQUEST);
    $id = @$_GET['id'];
   // determine if short url is valid
    $query = "SELECT id,long_url,shorturl_code FROM shorturl WHERE shorturl_code = '". $id ."'"; 
   //$stringdd = "SELECT url FROM redirects WHERE string = '". $string ."'";
	$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
	$row = mysql_fetch_array($result);
	$long_url = $row['long_url'];
	$url_id = $row['id'];
	$shorturl_code = $row['shorturl_code'];
	 
	 
	$num_results = mysql_num_rows($result);
   if ( $num_results > 0  ) {
    header("HTTP/1.1 301 Moved Permanently");
	$today = date("Y-m-d H:i:s");
	 $msisdn=@$_SERVER['HTTP_MSISDN'];			    
	 $httpuseragent=$_SERVER['HTTP_USER_AGENT'];
	 //echo "<br>";
	// die();
	$arr = explode("(",$httpuseragent);
	 // $userAgent=  $arr[0];
	$arrnext = explode(")",$arr[1]);
	$os = $arrnext[0];
//	$userAgent=  $arrnext[1];
	
  //echo "+++";
  //  
  // print_r($arrnext[0]);
  //echo "*****";
  //  print_r($arrnext[1]);
  //die();		
  //echo "<hr>..";

$browser="";
     if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("MSIE"))){$browser="ie";}
else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("Presto"))){$browser="opera";}
else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("CHROME"))){$browser="chrome";}
else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("SAFARI"))){$browser="safari";}
else if(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]),strtolower("FIREFOX"))){$browser="firefox";}
else {$browser="other";}
$userAgents = $browser;
  
	  $query = "INSERT INTO track_url(t_id,msisdn,useragent,os,datetime,shorturl_id,shorturl_code,long_url) values(NULL,'$msisdn','$userAgents','$os','$today','$url_id','$shorturl_code','$long_url')";
	
// die();
    $result = mysql_query($query) or die ("Error in query: $query " . mysql_error());	   
	// die();
     header("Location: ". $long_url );
	//exit;
   } else {
    // redirect to submit page or short URL not found page.
	
	//echo "amar...........".$num_results;
   }
mysql_close($con);
 ?> 